﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxHtmlEditor;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Controls;

public partial class CompanyReport : ICPGeneralPage
{
    public DataTable m_dtSource = new DataTable();
    public CompanyProfileData m_cpd = null;
    public DateTime m_dtmStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
    public DateTime m_dtmEnd = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
	public String m_sxFilterSeries = String.Empty;
	
    #region Properties
    public int m_iCompanyId
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["cid"]))
            {
                return int.Parse(Request.QueryString["cid"]);
            }
            else
            {
                return 0;
            }
        }
    }

    public int m_iReportId
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["rpt"]))
            {
                return int.Parse(Request.QueryString["rpt"]);
            }
            else
            {
                return 0;
            }
        }
    }
    #endregion Properties

    #region Event Handlers
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Company Reports";
        if (!IsPostBack && !IsCallback)
        {
            Session[KSession.CompanyReport.JASAJARINGAN_STARTDATE] = m_dtmStart;
            Session[KSession.CompanyReport.JASAJARINGAN_ENDDATE] = m_dtmEnd;
            Session[KSession.CompanyReport.FILTER_SERIES] = m_sxFilterSeries;
        }
        else
        {
            m_dtmStart = Convert.ToDateTime(Session[KSession.CompanyReport.JASAJARINGAN_STARTDATE]);
            m_dtmEnd = Convert.ToDateTime(Session[KSession.CompanyReport.JASAJARINGAN_ENDDATE]);
            m_sxFilterSeries = Session[KSession.CompanyReport.FILTER_SERIES].ToString();
        }
        
        InitReport();

    }

    protected void chrMain_CustomCallback(object sender, DevExpress.XtraCharts.Web.CustomCallbackEventArgs e)
    {
        String[] sxSplitted = e.Parameter.Split(new string[] { KGlobal.DELIMITER }, StringSplitOptions.None);
		String sxFilteredSeries = sxSplitted[2];
		
        Session[KSession.CompanyReport.JASAJARINGAN_STARTDATE] = m_dtmStart = DateTime.Parse(sxSplitted[0]);
        Session[KSession.CompanyReport.JASAJARINGAN_ENDDATE] = m_dtmEnd = DateTime.Parse(sxSplitted[1]);
        Session[KSession.CompanyReport.FILTER_SERIES] = m_sxFilterSeries = sxFilteredSeries;
        CompanyReportManager.GenerateChart(chrMain, m_iReportId, m_iCompanyId, m_dtmStart, m_dtmEnd,
            m_sxFilterSeries);
    }
    protected void dgMain_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        String[] sxSplitted = e.Parameters.Split(new string[] { KGlobal.DELIMITER }, StringSplitOptions.None);
		String sxFilteredSeries = sxSplitted[2];
		
        Session[KSession.CompanyReport.JASAJARINGAN_STARTDATE] = m_dtmStart = DateTime.Parse(sxSplitted[0]);
        Session[KSession.CompanyReport.JASAJARINGAN_ENDDATE] = m_dtmEnd = DateTime.Parse(sxSplitted[1]);
        Session[KSession.CompanyReport.FILTER_SERIES] = m_sxFilterSeries = sxFilteredSeries;
        CompanyReportManager.GenerateGrid(dgMain, m_iReportId, m_iCompanyId, m_dtmStart, m_dtmEnd);
    }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        String sxReportName = CompanyReportManager.GetReportName(m_iReportId);

        m_dtmStart = Convert.ToDateTime(Session[KSession.CompanyReport.JASAJARINGAN_STARTDATE]);
        m_dtmEnd = Convert.ToDateTime(Session[KSession.CompanyReport.JASAJARINGAN_ENDDATE]);

        String sxStartDate = m_dtmStart.ToString("dd-MMMM-yyyy");
        String sxEndDate = m_dtmEnd.ToString("dd-MMMM-yyyy");
        String sxFilename = String.Format("{0} - {1} ({2} to {3})", m_cpd.sxCompanyName, sxReportName, sxStartDate, sxEndDate);
        dgExporter.WriteXlsToResponse(sxFilename);

        //btnExport.Text = BulkInsertManager.TryTheBulkInsert();

    }
    #endregion Event Handlers

    #region Custom Methods
    private void InitReport()
    {
        pnlCompanyTitle.InnerHtml = InitCompanyProfile();

        CompanyReportManager.GenerateGrid(dgMain, m_iReportId, m_iCompanyId, m_dtmStart, m_dtmEnd);
        CompanyReportManager.GenerateChart(chrMain, m_iReportId, m_iCompanyId, m_dtmStart, m_dtmEnd,
            m_sxFilterSeries);
        
    }

    private String InitCompanyProfile()
    {
        Cached.CompanyTable ccCompany = new Cached.CompanyTable();
        m_cpd = ccCompany.GetCompanyProfileData(m_iCompanyId);

        String sxOutput = String.Empty;

        sxOutput = String.Format(
            "<a href=\"CompanyProfile.aspx?cid={0}\"><img src=\"upload/company/{0}.jpg\" alt=\"\" onerror=\"setEmptyCompanyAvatar(this);\" class=\"cpP\" /><span id=\"cp_Header\">{1}</span></a>",
            m_cpd.ID.ToString(), m_cpd.sxCompanyName);
        return sxOutput;
    }

    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }


    #endregion Custom Methods

    
}
